package com.rzico.sbl.ui.member

import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityMemberPromoterBinding
import com.rzico.sbl.model.PromoterData
import com.rzico.sbl.viewmodel.MemberViewModel
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.hideSoftInput
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.slimadapter.SlimAdapter

class MemberPromoterActivity : BaseActivity(R.layout.activity_member_promoter) {

    private val mBinding by lazy { ActivityMemberPromoterBinding.bind(getRootView()) }

    override fun getViewModel() = get<MemberViewModel>()

    private var mKeyWord = ""

    override fun initData() {
        setToolbarVisibility(isVisible = false)

        initLayout()
        initSearch()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData()
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关推广员信息！"
                }

                recycleList.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        isLoadingMore
                            .conditionFalse {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                    }
                )

                recycleList.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<PromoterData>(R.layout.item_member_promoter) { jector, bean, _ ->
                        jector
                            .text(R.id.item_promoter_name, bean.name.orEmpty("无"))
                            .text(R.id.item_promoter_phone, bean.mobile.orEmpty("无"))
                            .text(
                                R.id.item_promoter_job,
                                when (bean.type) {
                                    "0" -> "员工"
                                    "1" -> "会员"
                                    else -> ""
                                }
                            )

                            .with<ImageView>(R.id.item_promoter_logo) {
                                it.loadCircleImage(bean.logo, R.mipmap.default_user)
                            }

                            .clicked {
                                LiveEventBus
                                    .get("promoterChange")
                                    .post("${bean.id},${bean.name}")

                                finishView()
                            }
                    }
                    .attachTo(recycleList)
            }
    }

    private fun initSearch() {
        mBinding
            .promoterSearch
            .apply {
                searchEdit.hint = "请输入姓名、手机号"
                searchEdit.doOnTextChanged { text, _, _, _ ->
                    text?.let {
                        searchClose.isGone = it.isEmpty()

                        if (it.isEmpty() && mKeyWord.isNotEmpty()) {
                            mKeyWord = ""
                            updateList()
                        }
                    }
                }

                searchEdit.setOnEditorActionListener { _, actionId, _ ->
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        hideSoftInput() //隐藏软键盘

                        searchEdit.text
                            .isBlank()
                            .conditionElse(
                                actionTrue = { showToast("请输入姓名、手机号") },
                                actionFalse = {
                                    mKeyWord = searchEdit.textTrim
                                    updateList()
                                }
                            )
                    }
                    return@setOnEditorActionListener false
                }

                searchClose.oneClick { searchEdit.setText("") }
                searchHint.oneClick {
                    searchEdit.text
                        .isBlank()
                        .conditionElse(
                            actionTrue = { showToast("请输入姓名、手机号") },
                            actionFalse = {
                                mKeyWord = searchEdit.textTrim
                                updateList()
                            }
                        )
                }
            }
    }

    /**
     * 配送人员列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .promoterList(
                page = index,
                keyWord = mKeyWord,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData()
    }

}